import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yp/config/config.dart';
import 'package:yp/services/api.dart';
import '../../helper.dart';
import '../../router.dart';
import 'state.dart';

class MyCollectController extends GetxController {
  final state = MyCollectState();

  @override
  void onInit() {
    super.onInit();
    getColoectShopList();
    // 获取用户信息
    var params = {
      'page_no': state.pageIndex,
      'page_size': state.pageSize,
      'orderBy': '',
      // 'ec_shop_num_id': SpUtil.getInt('storeShopNum'),
      "ec_shop_num_id": SpUtil.getObject(Config.GET_SHOP_INFO)['shop_id'],
      'access_token': SpUtil.getString('token'),
      "use_platform":2,
      "channel_num_id":99,
    };
    Api.getCollectLists(params).listen((event) {
      print('收藏的商品----$event');

      if (event['code'] == 0 && event['list'] != null) {
        state.collectLists = event['list'];
      } else {}
      update();
    });
  }

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
  }

  // 获取收藏店铺列表
  void getColoectShopList() {
    var params = {
      'page': 1,
      'page_size': 100,
      'usr_num_id': SpUtil.getInt('empeNumId'),
      // "ec_shop_num_id": SpUtil.getObject(Config.GET_SHOP_INFO)['shop_id'],
      'access_token': SpUtil.getString('token'),
    };
    Api.goCollectShopDetail(params).listen((event) {
      print('收藏的店铺----$event');
      if (event['code'] == 0 && event['favorite_shop_list'] != null) {
        state.shopCollectLists = event['favorite_shop_list'];
      } else {
        state.shopCollectLists = [];
      }
      update();
    });
  }

  // ignore: slash_for_doc_comments
  /**
  * David
  * 删除收藏的商品
  */
  void collecDelete(BuildContext context, int styleNumId, int ecShopNumid) {
    print('styleNumId-------$styleNumId');
    print('ecShopNumid-------$ecShopNumid');
    toast("删除中...");
    var params = {
      'style_num_id': styleNumId,
      'ec_shop_num_id': ecShopNumid,
      'access_token': SpUtil.getString('token'),
    };
    Api.collectListsDelete(params).listen((event) {
      if (event['code'] == 0) {
        toast("删除成功...");
        onInit();
        update();
      } else {}
    });
  }

// ignore: slash_for_doc_comments
  /**
  * David
  * 删除收藏的店铺
  */
  void collecShopDelete(BuildContext context, int ecShopNumid) {
    toast("删除中...");
    var params = {
      'ec_shop_num_id': ecShopNumid,
      'usr_num_id': SpUtil.getInt('empeNumId'),
      'access_token': SpUtil.getString('token'),
    };
    Api.favoriteShopRemove(params).listen((event) {
      if (event['code'] == 0) {
        toast("删除成功...");
        onInit();
        update();
      } else {}
    });
  }

  void getAndGoThirdShop(int shopId) {
    var params = {
      "data_sign": 0,
      "channel_num_id": 99,
      "use_platform": 2,
      "access_token": SpUtil.getString("token"),
      "ec_shop_num_id": shopId.toString(),
    };
    SpUtil.putInt('thirdStoreShopNum', shopId);
    Api.getShopInfo(params).listen((event) {
      if (event != null && event['code'] == 0) {
        if (event['ec_shop_infos'].length > 0) {
          SpUtil.remove(Config.GET_SHOP_INFO);
          var shoplistes = event['ec_shop_infos'];
          SpUtil.putObject(Config.GET_SHOP_INFO, {
            'shop_name': shoplistes[0]['ec_shop_name'],
            'shop_id': shoplistes[0]['ec_shop_num_id'],
            'super_ec_shop_num_id': shoplistes[0]['super_ec_shop_num_id'],
            'is_get_home': shoplistes[0]['is_get_home'],
            'is_express': shoplistes[0]['is_express'],
            'service_telphone': shoplistes[0]['service_telphone'],
            'is_pick_up': shoplistes[0]['is_pick_up'],
            'adr': shoplistes[0]['adr'],
            'telphone': shoplistes[0]['telphone'],
            'pickAdrId': shoplistes[0]['sub_unit_num_id'],
            'offShopName': shoplistes[0]['sub_unit_name'],
            'offShopadr': shoplistes[0]['sub_unit_adr'],
            'sub_unit_num_id': shoplistes[0]['sub_unit_num_id'],
            'isSupporySacn': true,
            'free_start_time': '1970-01-01' + shoplistes[0]['free_start_time'],
            'is_express_shops':
                shoplistes[0]['cloud_ec_shop_num_id'] == 1 ? true : false,
            'prv_num_id': shoplistes[0]['prv_num_id'],
            'prv_name': shoplistes[0]['prv_name'],
            'city_num_id': shoplistes[0]['city_num_id'],
            'city_name': shoplistes[0]['city_name'],
            'city_area_num_id': shoplistes[0]['city_area_num_id'],
            'city_area_name': shoplistes[0]['city_area_name'],
            'ec_shop_logo': shoplistes[0]['ec_shop_logo']
          });
          SpUtil.putInt(Config.IS_CHANGE_SHOP,
              SpUtil.getInt('storeShopNum') == shopId ? 0 : 1);
          Get.offAllNamed(RouteConfig.main, arguments: {'isChangeShop': true});
        }
      } else {
        toast(event['massage']);
      }
    });
  }

  /**
  * David
  * 购物，内容，tab切换
  */
  void tabSelect(BuildContext context, int index) {
    if (index == 1) {
      var params = {
        'page_no': state.pageIndex,
        'page_size': state.pageSize,
        'orderBy': '',
        // 'ec_shop_num_id': SpUtil.getInt('storeShopNum'),
        "ec_shop_num_id": SpUtil.getObject(Config.GET_SHOP_INFO)['shop_id'],
        'access_token': SpUtil.getString('token'),
        "use_platform":2,
        "channel_num_id":99,
      };
      Api.getCollectLists(params).listen((event) {
        if (event['code'] == 0) {
          state.collectLists = event['list'];
        } else {}
        update();
      });
    } else {
      getColoectShopList();
    }
    state.shoppingSelect = !state.shoppingSelect;
    update();
  }

  // ignore: slash_for_doc_comments
  /**
  * David
  * 商品加入购物车
  */
  void collectAddShoopingCart(
    BuildContext context,
    int ec_shop_num_id,
    int style_num_id,
    int ec_style_num_id,
  ) {
    var params = {
      "use_platform": 2, //平台
      "usr_num_id": SpUtil.getInt('empeNumId'), //会员ID
      "emp_num_id": 0, //操作员ID,移动端无属性,默认传0
      "channel_num_id": 99, //订单渠道：小程序 98，App 99
      "quantity": 1.0, //数量
      // "ec_shop_num_id": ec_shop_num_id, //店铺编码
      "ec_shop_num_id": SpUtil.getObject(Config.GET_SHOP_INFO)['shop_id'],
      "item_num_id": style_num_id, //商品编码
      "style_num_id": style_num_id, //款式编码
      "ec_style_num_id": ec_style_num_id, //款式唯一编码
      'access_token': SpUtil.getString('token'),
      // "prv_num_id": 320000, //省编码
      // "city_num_id": 320500, //市编码
      // "city_area_num_id": 320583, //区编码
    };
    Api.addShoppingCart(params).listen((event) {
      if (event['code'] == 0) {
        toast("添加成功...");
        onInit();
        update();
      } else {}
    });
  }

  getrecommendGoods() {
    var params = {
      "usr_num_id": SpUtil.getInt('empeNumId'),
      'access_token': SpUtil.getString('token'),
      "use_platform": 2,

      "is_recommend": 1, //死值,推荐商品
      // "shop_id": SpUtil.getInt('storeShopNum'),
      "shop_id": SpUtil.getObject(Config.GET_SHOP_INFO)['shop_id'],
      // "prv_num_id": "320000",
      // "city_num_id": "320500",
      // "city_area_num_id": "320583",
      "page_no": 1,
      "page_size": 10,
      "order_by": "sold_quantity desc"
    };
    state.TJlist = [];
    update();
    Api.recommendGoods(params).listen((event) {
      if (event["code"] == 0) {
        if (event['lists'] != null && event['lists'].length > 0) {
          state.TJlist = event['lists'];
        }
        update();
      }
    });
  }

// 进入商品详情
  void enterGoodsDetail(int style_num_id, int ec_style_num_id, int cat_id,
      int default_item_num_id, int shop_num_id, int is_pre_sale) {
    String itemNumId = default_item_num_id.toString();
    String styleNumId = style_num_id.toString();
    // String shopId = shop_num_id.toString();
    // String cat1Id = goodsJson['cat1_id'].toString();
    String cat1Id = cat_id.toString();
    String ecStyleNumId = ec_style_num_id.toString();
    Get.toNamed(RouteConfig.goodsDetail, arguments: {
      'itemNumId': itemNumId,
      'styleNumId': styleNumId,
      'shopId': SpUtil.getObject(Config.GET_SHOP_INFO)['shop_id'].toString(),
      // 'cat1Id': cat1Id,
      'ecStyleNumId': ecStyleNumId,
      'typeIndex': is_pre_sale,
    });
  }
}
